from django.db import models
from apps.user.models import SystemUser


class Projects(models.Model):
	name = models.CharField(max_length=50, verbose_name="项目名")
	mark = models.CharField(max_length=10, verbose_name="项目代号")
	protocol = models.SmallIntegerField(null=False, verbose_name="协议")
	host = models.URLField(max_length=200, null=False, verbose_name="域名")
	desc = models.CharField(max_length=300, null=False, verbose_name="描述信息")
	status = models.BooleanField(default=True)
	# creator = models.ForeignKey(SystemUser, on_delete=models.DO_NOTHING, verbose_name="创建人")
	create_time = models.DateTimeField(auto_now_add=True)
	update_time = models.DateTimeField(auto_now=True)
	deleted = models.BooleanField(default=False)

	class Meta:
		db_table = "tiresias_project"
		verbose_name = "项目"
		verbose_name_plural = verbose_name

	def __str__(self):
		return self.name


class SystemDictData(models.Model):
	dict_name = models.CharField(max_length=50, verbose_name="字典名")
	dict_value = models.CharField(max_length=100, verbose_name="字典值")
	dict_label = models.CharField(max_length=100, verbose_name="字典类型")
	status = models.BooleanField(default=True)
	desc = models.CharField(max_length=300, verbose_name="描述信息", null=True)
	# creator = models.ForeignKey(SystemUser, on_delete=models.DO_NOTHING, verbose_name="创建人")
	create_time = models.DateTimeField(auto_now_add=True)
	update_time = models.DateTimeField(auto_now=True)
	deleted = models.BooleanField(default=False)

	class Meta:
		db_table = "tiresias_sys_dict_data"
		verbose_name = "系统字典"
		verbose_name_plural = verbose_name

	def __str__(self):
		return self.dict_name


class Apis(models.Model):
	name = models.CharField(max_length=50, verbose_name="接口名")
	url = models.URLField(max_length=200, verbose_name="接口地址", null=False)
	project = models.ForeignKey(Projects, on_delete=models.DO_NOTHING, verbose_name="所属项目")
	desc = models.CharField(max_length=1000, verbose_name='描述信息')
	status = models.BooleanField(default=False)
	create_time = models.DateTimeField(auto_now_add=True)
	update_time = models.DateTimeField(auto_now=True)
	# creator = models.ForeignKey(SystemUser, on_delete=models.DO_NOTHING, verbose_name="创建人")
	deleted = models.BooleanField(default=False)

	class Meta:
		db_table = "tiresias_api_data"
		verbose_name = "系统字典表"
		verbose_name_plural = verbose_name

	def __str__(self):
		return self.name


class ApiCase(models.Model):
	"""
	接口用例表
	"""
	api = models.ForeignKey(Apis, on_delete=models.DO_NOTHING, verbose_name='所属接口')
	request_type = models.ForeignKey(SystemDictData, on_delete=models.DO_NOTHING, related_name='request_type',
									 verbose_name='请求方式')
	param_type = models.ForeignKey(SystemDictData, on_delete=models.DO_NOTHING, related_name='param_type',
								   verbose_name='参数类型')
	job_type = models.ForeignKey(SystemDictData, on_delete=models.DO_NOTHING, related_name='job_type',
								 verbose_name='任务类型')
	expect_type = models.ForeignKey(SystemDictData, on_delete=models.DO_NOTHING, related_name='expect_type',
									verbose_name='预期类型')
	case_name = models.CharField(max_length=50, verbose_name="用例名")
	headers = models.CharField(max_length=1000, verbose_name="请求头")
	param = models.CharField(max_length=1000, verbose_name="请求参数")
	job_cron = models.CharField(max_length=30, verbose_name="cron表达式")
	desc = models.CharField(max_length=1000, verbose_name='描述信息')
	expect = models.TextField(verbose_name="预期值")
	status = models.SmallIntegerField(default=0)
	task_id = models.CharField(max_length=100, verbose_name='调度ID')
	create_time = models.DateTimeField(auto_now_add=True)
	update_time = models.DateTimeField(auto_now=True)
	deleted = models.BooleanField(default=False)

	class Meta:
		db_table = 'tiresias_api_case'
		verbose_name = '用例表'
		verbose_name_plural = verbose_name

	def __str__(self):
		return self.case_name
